package com.wan.dao;


import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {

    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;
    private static Connection connection =null;

    // 用静态代码块，在类加载的时候执行，并且只执行一次
    static{
        InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");

    }

    // 获得数据库连接的方法
    public static Connection getConnection() {
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    // 执行sql语句查询的操作
    public static ResultSet execute(Connection connection,PreparedStatement preparedStatement,ResultSet rs,String sql,Object[] params) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        rs = preparedStatement.executeQuery();
        return rs;
    }

    // 执行sql语句增删改的操作
    public static int executeUpdate(Connection connection,PreparedStatement preparedStatement,Object[] params,String sql) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        int impactRows = preparedStatement.executeUpdate();
        return impactRows;
    }

    // 释放资源
    public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet rs) {
        boolean flag = true;
        if(connection != null ) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        if(preparedStatement != null ) {
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        if(rs != null ) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }
}
